<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 表单 -->
      <div style="margin: 60px"></div>
      <el-form
        :label-position="labelPosition"
        label-width="0px"
        :model="formData"
        ref="loginForm"
        :rules="loginFormRules"
        class="login_form"
      >
        <el-form-item prop="username">
          <el-input
            class="usernameinp"
            v-model="formData.username"
            prefix-icon="el-icon-user"
            @keyup.enter="onSubmit"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            class="usernameinp"
            v-model="formData.password"
            prefix-icon="el-icon-key"
            type="password"
            @keyup.enter.native="onSubmit"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="onSubmit" class="btns"
            >立即登录</el-button
          >
          <el-button class="btns" type="warning">现在注册</el-button>
        </el-form-item>
        <el-form-item class="forgetPsd">
          <a href=":script">忘记密码!</a>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { Service } from '../service'
export default {
  data() {
    return {
      labelPosition: 'right',
      formData: {
        username: 'admin',
        password: '123456',
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 11,
            message: '用户名长度在3-11位',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '密码长度在6-15位',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  methods: {
    onSubmit() {
      // 校验表单对象是否通过验证
      this.$refs.loginForm.validate(async (isPass) => {
        if (isPass) {
          const res = await Service.login(
            this.formData.username,
            this.formData.password
          )
          if (
            res.data.meta.msg === '登录成功' &&
            res.data.meta.status === 200
          ) {
            sessionStorage.setItem('dianshangToken', res.data.data.token)
            this.$router.push('/home')
            return this.$message.success(res.data.meta.msg)
          } else {
            return this.$message.error(res.data.meta.msg)
          }
        } else {
          return this.$message.error('登陆失败,数据未完善')
        }
      })
    },
    
  },
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b5b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  background: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
.login_form {
  position: absolute;
  top: 50%;
  transform: translateY(-30%);
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}
.btns {
  text-align: center;
  margin: 0 auto;
}
.forgetPsd {
  text-align: right;
}
.forgetPsd a {
  font-size: 12px;
  text-decoration: none;
  color: darkorange;
}
.usernameinp {
  font-size: 18px;
  color: #555;
}
.el-input__prefix {
  color: #555;
}
</style>